﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Backend.BLL;
using Backend.DTO;
using System.Globalization;

namespace ReSeSSApplication
{
    public partial class Profile : System.Web.UI.Page
    {
        UserBLL aUserBll = new UserBLL();
        protected UserDTO loggedInUserDTO = new UserDTO();
        UserDTO aUser = new UserDTO();

        protected void Page_Load(object sender, EventArgs e)
        {
            AlertSuccess.Visible = false;
            AlertError.Visible = false;
            txtLoginId.Enabled = false;

            loggedInUserDTO = (UserDTO)Session["LoggedInUserDTO"];
            RedirectUser(loggedInUserDTO);

            if (!IsPostBack)
            {
                PoppulateDropDowns();
            }

            aUser = aUserBll.GetUserById(loggedInUserDTO.Id);

            if (!IsPostBack)
            {
                if (aUser != null)
                {
                    txtLoginId.Text = aUser.LoginId;
                    txtFirstName.Text = aUser.FirstName;
                    txtLastName.Text = aUser.LastName;
                    txtEmail.Text = aUser.Email;

                    ddlMonth.SelectedValue = aUser.DateOfBirth.Month.ToString();
                    ddlDay.SelectedValue = aUser.DateOfBirth.Day.ToString();
                    ddlYear.SelectedValue = aUser.DateOfBirth.Year.ToString();

                    if (aUser.Gender == "Male")
                    {
                        rdoGenderMale.Checked = true;
                        rdoGenderFemale.Checked = false;
                    }
                    else if (aUser.Gender == "Female")
                    {
                        rdoGenderMale.Checked = false;
                        rdoGenderFemale.Checked = true;
                    }
                    else
                    {
                        rdoGenderMale.Checked = false;
                        rdoGenderFemale.Checked = false;
                    }
                }
                else
                {
                    AlertError.InnerText = "Invalid user to edit";
                    AlertError.Visible = false;
                }
            }
        }

        protected void UpdatePofileButton_Click(object sender, EventArgs e)
        {
            string firstName = txtFirstName.Text.Trim();
            string lastName = txtLastName.Text.Trim();
            DateTime dateOfBirth;
            string email = txtEmail.Text.Trim();
            int month = Convert.ToInt32(ddlMonth.SelectedItem.Value);
            int day = Convert.ToInt32(ddlDay.SelectedItem.Value);
            int year = Convert.ToInt32(ddlYear.SelectedItem.Value);
            string gender = string.Empty;

            if (rdoGenderMale.Checked) { gender = rdoGenderMale.Text; }
            else if (rdoGenderFemale.Checked) { gender = rdoGenderFemale.Text; }

            if (firstName == string.Empty || lastName == string.Empty || email == string.Empty)
            {
                AlertError.InnerText = "Required field cannot be empty";
                AlertError.Visible = true;
                return;
            }

            try
            {
                dateOfBirth = Convert.ToDateTime(year + "-" + month + "-" + day);
            }
            catch (Exception ex)
            {
                AlertError.InnerText = "Invalid Birthday entered";
                AlertError.Visible = true;
                return;
            }

            try
            {
                if (!string.IsNullOrEmpty(firstName) && firstName != aUser.FirstName)
                {
                    aUserBll.UpdateUser("FirstName", firstName, aUser.Id);
                    aUser.FirstName = firstName;
                }
                if (!string.IsNullOrEmpty(lastName) && lastName != aUser.LastName)
                {
                    aUserBll.UpdateUser("LastName", lastName, aUser.Id);
                    aUser.LastName = lastName;
                }
                if (!string.IsNullOrEmpty(email) && email != aUser.Email)
                {
                    aUserBll.UpdateUser("Email", email, aUser.Id);
                    aUser.Email = email;
                }
                if (dateOfBirth != aUser.DateOfBirth)
                {
                    aUserBll.UpdateUser("DateOfBirth", dateOfBirth.Year + "-" + dateOfBirth.Month + "-" + dateOfBirth.Day, aUser.Id);
                    aUser.DateOfBirth = dateOfBirth;
                }
                if (gender != aUser.Gender)
                {
                    aUserBll.UpdateUser("Gender", gender, aUser.Id);
                    aUser.Gender = gender;
                }

                Session["LoggedInUserDTO"] = aUser;
                AlertSuccess.InnerText = "Profile updated successfully";
                AlertSuccess.Visible = true;
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
                return;
            }
        }

        private void PoppulateDropDowns()
        {
            for (int i = 0; i <= 12; i++)
            {
                if (i == 0)
                {
                    ListItem aListItem = new ListItem("Select Month", "0");
                    ddlMonth.Items.Add(aListItem);
                }
                else
                {
                    ListItem aListItem = new ListItem(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i), i.ToString());
                    ddlMonth.Items.Add(aListItem);
                }
            }

            for (int i = 0; i <= 31; i++)
            {
                if (i == 0)
                {
                    ListItem aListItem = new ListItem("Select Day", "0");
                    ddlDay.Items.Add(aListItem);
                }
                else
                {
                    ddlDay.Items.Add(i.ToString());
                }
            }

            int startYear = DateTime.Now.Year;
            for (int i = startYear; i > (startYear - 100); i--)
            {
                if (i == startYear)
                {
                    ListItem aListItem = new ListItem("Select Year", "0");
                    ddlYear.Items.Add(aListItem);
                }

                ddlYear.Items.Add(i.ToString());
            }
        }

        protected void RedirectUser(UserDTO userDTO)
        {
            if (userDTO == null)
            {
                Response.Redirect("Index.aspx");
            }
        }
    }
}